#!/bin/bash

version=$(sed -n 's/.*Version.*=.*"\([^"]*\)".*/\1/p' config/version.go)
revision=$(sed -n 's/.*Revision.*=.*"\([^"]*\)".*/\1/p' config/version.go)
test -z "$version" && exit 1
test -z "$revision" && exit 1
section=lhm/utils
priority=optional
timestamp="$(LC_ALL=C date -R)"
debversion=${version}-msb1

test -d deb/ && { echo "deb/ already exists!" ; exit 1 ; }
mkdir deb || exit 1
make doc || exit 1
make man || exit 1
hg archive -t tar -p go-susi-$version deb/go-susi_$version.orig.tar
mkdir -p deb/go-susi-$version/{doc,config}
cp doc/go-susi-manual.pdf deb/go-susi-$version/doc/go-susi-$version-manual.pdf
cp doc/go-susi-manual.odt deb/go-susi-$version/doc/go-susi-$version-manual.odt
cp doc/go-susi-manual.xhtml deb/go-susi-$version/doc/go-susi-$version-manual.xhtml
cp doc/*.5 doc/*.1 deb/go-susi-$version/doc/
cp config/version.go deb/go-susi-$version/config/
cd deb || exit 1

# Replace outdated documentation files in archive with current versions.
# Of course it would be better to keep the documentation in the hg repo up-to-date,
# but I don't want to enable the CI server to do automated checkins into the repository.
tar --delete --file=go-susi_$version.orig.tar go-susi-$version/doc/go-susi-manual.{pdf,xhtml,odt}
tar --append --file=go-susi_$version.orig.tar go-susi-$version/doc

# Add version.go to the archive.
tar --append --file=go-susi_$version.orig.tar go-susi-$version/config/version.go

gzip -9 go-susi_$version.orig.tar


tar xzf go-susi_$version.orig.tar.gz
cd go-susi-$version || exit 1
mkdir debian

echo >debian/changelog "go-susi ($debversion) tramp; urgency=low

  * Version $version

 -- Matthias S. Benkmann <matthias@winterdrache.de>  $timestamp
"

echo >debian/control "Source: go-susi
Section: $section
Priority: $priority
Maintainer: Matthias S. Benkmann <matthias@winterdrache.de>
Build-Depends: golang | golang-stable | golang-tip
Standards-Version: 3.9.4

Package: go-susi
Architecture: any
Depends: ldap-utils, libc6
Conflicts: gosa-si-server, gosa-si-client, gosa-si-common
Replaces: gosa-si-server, gosa-si-client, gosa-si-common
Provides: gosa-si-server, gosa-si-client, gosa-si-common
Suggests: gosa, tidy
Description: Support infrastructure daemon for GOsa
 A stable and fast replacement for gosa-si-server/gosa-si-client, the
 support infrastructure daemon required by GOsa 2.7.
"

# do not create server.conf. It is created by dhclient-exit-hook from
# the template server.conf.in

echo >logrotate-gosa-si-server '/var/log/gosa-si-server.log {
    daily
    rotate 4
    compress
    missingok
    postrotate
        pkill -SIGUSR1 gosa-si-server > /dev/null || true
    endscript
}
'

echo /etc/logrotate.d/gosa-si-server >debian/conffiles

echo >debian/copyright 'This package was debianized by Matthias S.Benkmann <mux2005@gmail.com> on
Tue, 02 Apr 2013 19:04:30 +0200.

It was downloaded from http://go-susi.googlecode.com/

Copyright: 

    Copyright (C) 2012-2013 Matthias S. Benkmann <mux2005@gmail.com>

License:

    This package is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
 
    This package is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    along with this package; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

On Debian systems, the complete text of the GNU General
Public License can be found in /usr/share/common-licenses/GPL.
'

echo >debian/postinst '#!/bin/sh -e

# We exit unless the package is being configured
case "$1" in
        abort*upgrade)         exit 0;;
        abort*remove)          exit 0;;
        abort*deconfigure)     exit 0;;
        configure) ;;
        *)                     exit 0;
esac

# Fix permission
[ -f /etc/gosa-si/server.conf ] && chmod go-rwx /etc/gosa-si/server.conf

# Restart daemon
invoke-rc.d gosa-si restart

exit 0
'
chmod a+rx debian/postinst

echo >debian/rules "#!/usr/bin/make -f
clean:
	make almostclean
	rm -f debian/files
	rm -rf dist/

DEBVERSION := \$(shell dpkg-parsechangelog | sed -n -e 's/^Version: //p')

build:
	umask 022
	sed -i 's/^const.*Version.*=.*/const Version = \"\$(DEBVERSION)\"/' config/version.go
	go build main/go-susi.go
	go build main/sibridge.go
	go build main/encrypt.go
	go build main/decrypt.go
	# DO NOT STRIP BINARIES! THIS BREAKS THEM WITH GO 1.1 (AT LEAST ON ARM)
	chmod a+rx main/gosa-si-server main/generate_* main/send_* main/update_config_files main/initrd_autopack
	mkdir -p dist/etc/gosa-si dist/etc/logrotate.d dist/usr/lib/go-susi/ dist/usr/sbin
	mkdir -p dist/etc/default dist/etc/init.d
	mkdir -p dist/usr/share/doc/go-susi
	mkdir -p dist/usr/share/man/man1
	mkdir -p dist/usr/share/man/man5
	cp debian/copyright dist/usr/share/doc/go-susi
	cp debian/changelog dist/usr/share/doc/go-susi/changelog.Debian
	cp doc/*.odt doc/*.pdf doc/*.xhtml dist/usr/share/doc/go-susi/
	cp doc/*.1 dist/usr/share/man/man1
	cp doc/*.5 dist/usr/share/man/man5
	gzip -9 dist/usr/share/doc/go-susi/changelog.Debian
	cp main/generate_* main/send_user_msg main/update_config_files dist/usr/lib/go-susi/
	cp main/trigger_action main/registered dist/usr/lib/go-susi/
	cp main/activated main/detect_hardware dist/usr/lib/go-susi/
	cp main/fai_progress main/fai_savelog dist/usr/lib/go-susi/
	cp main/initrd_autopack dist/usr/lib/go-susi/
	cp go-susi dist/usr/lib/go-susi/gosa-si-server
	cp encrypt dist/usr/lib/go-susi/encrypt
	cp decrypt dist/usr/lib/go-susi/decrypt
	cp main/gosa-si-server dist/usr/sbin
	cp main/gosa-si.init dist/etc/init.d/gosa-si
	echo DEBUG=-vvv >dist/etc/default/gosa-si
	ln -snf ../lib/go-susi/gosa-si-server dist/usr/sbin/go-susi
	cp sibridge dist/usr/sbin/
	-cp logrotate-gosa-si-server dist/etc/logrotate.d/gosa-si-server
	echo >debian/files go-susi_\$(DEBVERSION)_\$(DEB_BUILD_ARCH).deb $section $priority

binary-arch:
        echo ====== binary-arch =========

binary-indep:
        echo ====== binary-indep =========

build-arch:
        echo ====== build-arch =========

build-indep:
        echo ====== build-indep =========

binary:
	umask 022
	mkdir dist/DEBIAN
	cp debian/postinst dist/DEBIAN/postinst
	cp debian/conffiles dist/DEBIAN/conffiles
	chmod a+rx dist/DEBIAN/postinst
	sed '/^\$\$/d;/^Build-Depends/d;s/^\\(Architecture:\\) .*/\\1 \$(DEB_BUILD_ARCH)/;s/Standards-Version:.*/Version: ${debversion}/' debian/control >dist/DEBIAN/control
	chown -R root: dist/
	chmod -R g-w,a+rX dist/
	dpkg-deb -b dist/ ../go-susi_${debversion}_\$(DEB_BUILD_ARCH).deb

"
chmod a+rx debian/rules
